package com.example.myapplicationndk;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.example.myapplicationndk.bean.Person;
import com.example.myapplicationndk.databinding.ActivityMainBinding;
import com.example.myapplicationndk.utils.JniHandle;
import com.example.myapplicationndk.utils.SaxHelper;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import com.retrofit.Study.SparseArrayClass;

public class MainActivity extends AppCompatActivity {

    static {
        System.loadLibrary("hello");
    }

    private ActivityMainBinding binding;
    ViewPager2 viewPager2;
    FragmentStateAdapter fragmentStateAdapter;
    TabLayout tabLayout;
    RecyclerView recyclerView;
    private TabLayoutMediator mediator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        int[] ints = new int[]{1, 2, 3, 4, 5};

        binding.tvJni.setText(callJavaMethodFromJni(JniHandle.class));

        Log.d("resultCode", this.getClass().getName());

        binding.btnEvent.setOnClickListener(v -> {
            Log.d("tag", "asd");
        });

        try {
            List<Person> personList = readxmlForSAX();
            Log.d("person", personList.toString());

            try {
                binding.tvJni.setText(personList.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.d("person", personList.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public ArrayList<Person> getPersons() throws Exception {
        InputStream is = getAssets().open("xml.xml");
        ArrayList<Person> persons = null;
        Person person = null;
        // 创建一个xml解析的工厂
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        // 获得xml解析类的引用
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(is, "UTF-8");
        // 获得事件的类型
        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    persons = new ArrayList<Person>();
                    break;
                case XmlPullParser.START_TAG:
                    if ("person".equals(parser.getName())) {
                        person = new Person();
                        // 取出属性值
                        int id = Integer.parseInt(parser.getAttributeValue(0));
                        person.setId(id);
                    } else if ("name".equals(parser.getName())) {
                        String name = parser.nextText();// 获取该节点的内容
                        person.setName(name);
                    } else if ("age".equals(parser.getName())) {
                        int age = Integer.parseInt(parser.nextText());
                        person.setAge(age);
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if ("person".equals(parser.getName())) {
                        persons.add(person);
                        person = null;
                    }
                    break;
            }
            eventType = parser.next();
        }
        return persons;
    }

    private ArrayList<Person> readxmlForSAX() throws Exception {
        //获取文件资源建立输入流对象
        InputStream is = getAssets().open("xml.xml");
        //①创建XML解析处理器
        SaxHelper ss = new SaxHelper();
        //②得到SAX解析工厂
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //③创建SAX解析器
        SAXParser parser = factory.newSAXParser();
        //④将xml解析处理器分配给解析器,对文档进行解析,将事件发送给处理器
        parser.parse(is, ss);
        is.close();
        return ss.getPersons();
    }

    private void tabLayout() {
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mediator = new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //这里可以自定义TabView
//                TextView tabView = new TextView(MainActivity.this);
//
//                int[][] states = new int[2][];
//                states[0] = new int[]{android.R.attr.state_selected};
//                states[1] = new int[]{};
//
//                int[] colors = new int[]{activeColor, normalColor};
//                ColorStateList colorStateList = new ColorStateList(states, colors);
//                tabView.setText(tabs[position]);
//                tabView.setTextSize(normalSize);
//                tabView.setTextColor(colorStateList);
//
//                tab.setCustomView(tabView);
            }
        });
        //要执行这一句才是真正将两者绑定起来
        mediator.attach();
    }

    // 从jni 获取字符串
    public native String fromJNIString();

    // 本地获取字符串加工
    public native String testString(String string);

    // 加法
    public native int add(int a, int b);

    // int数组处理
    public native String jniIntArrayToString(int[] intParams);

    public native String callJavaStaticMethodFromJni(Class classes);

    public native String callJavaMethodFromJni(Class classes);


    private void StudyTest() {
        SparseArrayClass.getSparseArrayTest();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }


}